package com.gxl.butt.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.gxl.butt.DAO.IbttokenDAO;
import com.gxl.butt.domain.BT_Token;
import com.gxl.butt.dto.bo.Token;
import com.gxl.butt.exception.CException;
import com.gxl.butt.service.ITokenService;
import com.gxl.butt.dto.vo.TokenBean;
import com.gxl.butt.utils.DateUtil;
import com.gxl.butt.utils.JSONUtils;
import com.gxl.butt.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.gxl.butt.value.ErrorMsg.*;

/**
 * Created by guoxiaolin on 2018/7/16.
 */
@Service(interfaceClass = ITokenService.class,retries = 0)
@Component
public class TokenServiceImpl implements ITokenService {

    @Autowired
    private IbttokenDAO ibttokenDAO;

    @Override
    @Transactional
    public JSONObject getToken(String account, String password,Long userid) {

        StringBuilder stringBuilder = new StringBuilder();
        String nowtime = String.valueOf(new Date().getTime());
        stringBuilder.append(account).append(password).append(nowtime);
        String token = MD5Util.toMD5(stringBuilder.toString());
        List<BT_Token> bt_tokens = ibttokenDAO.getTokenByUserId(userid);
        Token tokenBean = new Token();
        if (bt_tokens!=null && bt_tokens.size() > 0){
            BT_Token bt_token = bt_tokens.get(0);
            bt_token.setToken(token);
            bt_token.setGmtTime(nowtime);
            boolean flag =ibttokenDAO.updateToken(bt_token);
            if (flag){
                tokenBean.setId(bt_token.getId());
                tokenBean.setToken(bt_token.getToken());
                return JSONUtils.getRoot(0,SUCCESS_MSG,tokenBean);
            }
            return JSONUtils.getRoot(-1,FAILD_MSG,STR_IS_EMPTY);
        }else{
            BT_Token bt_token = new BT_Token();
            bt_token.setCreateTime(nowtime);
            bt_token.setGmtTime(nowtime);
            bt_token.setToken(token);
            bt_token.setUserid(userid);
            bt_token.setOpenid("0");
            Long tokenid = ibttokenDAO.addToken(bt_token);
            tokenBean.setId(tokenid);
            tokenBean.setToken(token);
            return JSONUtils.getRoot(0,SUCCESS_MSG,tokenBean);
        }
    }

    @Override
    @Transactional
    public JSONObject getToken(Long userid){

        List<BT_Token> bt_tokens = ibttokenDAO.getTokenByUserId(userid);

        if (bt_tokens != null && bt_tokens.size() > 0 ){
            return  JSONUtils.getRoot(0,SUCCESS_MSG,bt_tokens.get(0).getToken());
        }
        return  JSONUtils.getRoot(-1,TOKEN_IS_NULL,STR_IS_EMPTY);
    }

    @Override
    @Transactional
    public JSONObject updateToken(String account, String password,Long userid){

        StringBuilder stringBuilder = new StringBuilder();
        String nowtime = String.valueOf(new Date().getTime());
        stringBuilder.append(account).append(password).append(nowtime);
        String token = MD5Util.toMD5(stringBuilder.toString());
        List<BT_Token> bt_tokens = ibttokenDAO.getTokenByUserId(userid);

        if (bt_tokens!=null&&bt_tokens.size() > 0){
            BT_Token bt_token = bt_tokens.get(0);
            bt_token.setGmtTime(nowtime);
            bt_token.setToken(token);
            boolean flag = ibttokenDAO.updateToken(bt_token);
            Token tokenBean = new Token();
            if (flag){
                tokenBean.setToken(token);
                tokenBean.setId(bt_token.getId());
                return JSONUtils.getRoot(0,SUCCESS_MSG,tokenBean);
            }
        }
        return JSONUtils.getRoot(-1,UPDATE_TOKEN_LOST,STR_IS_EMPTY);

    }

    @Override
    @Transactional
    public JSONObject delToken(Long tokenid) {
        ibttokenDAO.delTokenByTokenId(tokenid);
        return JSONUtils.getRoot(0,SUCCESS_MSG,STR_IS_EMPTY);
    }

    @Override
    @Transactional
    public JSONObject getToken(String openid) {

        StringBuilder stringBuilder = new StringBuilder();
        String nowtime = String.valueOf(new Date().getTime());
        /*
        * 我就是无聊，我就要加密，打我啊
        * */
        stringBuilder.append(MD5Util.toMD5(openid+nowtime))
                .append(MD5Util.toMD5(nowtime+openid))
                .append(MD5Util.toMD5(UUID.randomUUID().toString()));
        String token = stringBuilder.toString().toUpperCase();

        List<BT_Token> bt_tokens = ibttokenDAO.getTokenByOpenid(openid);
        Token tokenBean = new Token();
        if (bt_tokens!=null && bt_tokens.size() > 0){
            BT_Token bt_token = bt_tokens.get(0);
            bt_token.setToken(token);
            bt_token.setGmtTime(nowtime);
            boolean flag =ibttokenDAO.updateToken(bt_token);
            if (flag){
                tokenBean.setId(bt_token.getId());
                tokenBean.setToken(bt_token.getToken());
                tokenBean.setGmt(DateUtil.timeStamp2DateInSSS(bt_tokens.get(0).getGmtTime(),"yyyy-MM-dd HH:mm"));
            }else{
                return JSONUtils.getRoot(-1,FAILD_MSG,STR_IS_EMPTY);
            }
        }else{
            BT_Token bt_token = new BT_Token();
            bt_token.setCreateTime(nowtime);
            bt_token.setGmtTime(nowtime);
            bt_token.setToken(token);
            bt_token.setOpenid(openid);
            bt_token.setUserid(0L);
            Long tokenid = ibttokenDAO.addToken(bt_token);
            tokenBean.setId(tokenid);
            tokenBean.setToken(token);
            tokenBean.setGmt(DateUtil.timeStamp2DateInSSS(bt_tokens.get(0).getGmtTime(),"yyyy-MM-dd HH:mm"));
        }
        return JSONUtils.getRoot(0,SUCCESS_MSG,tokenBean);
    }

    @Override
    @Transactional
    public JSONObject showToken(String openid) {

        List<BT_Token> bt_tokens = ibttokenDAO.getTokenByOpenid(openid);
        if (bt_tokens!=null && bt_tokens.size() > 0) {
            Token token = new Token();
            token.setId(bt_tokens.get(0).getId());
            token.setOpenid(bt_tokens.get(0).getOpenid());
            token.setToken(bt_tokens.get(0).getToken());
            token.setGmt(DateUtil.timeStamp2DateInSSS(bt_tokens.get(0).getGmtTime(),"yyyy-MM-dd HH:mm"));
            return JSONUtils.getRoot(0,SUCCESS_MSG,token);
        }
        return JSONUtils.getRoot(-1,TOKEN_IS_NULL,"");

    }
}
